package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/users")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 返回注册页面
     */
    @GetMapping("/register")
    public String showRegisterPage(Model model) {
        model.addAttribute("user", new User());
        return "register"; // 返回register.html视图
    }

    /**
     * 注册用户
     * @param user
     */
    @PostMapping("/register")
    public String register(@ModelAttribute User user) {
        userService.register(user);
        return "login"; // 重定向到登录页面
    }

    /**
     * 返回登录页面
     */
    @GetMapping("/login")
    public String showLoginPage(Model model) {
        model.addAttribute("user", new User());
        return "login"; // 返回login.html视图
    }

    /**
     * 处理登录请求
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user, HttpSession session) {
        User authenticatedUser = userService.authenticate(user.getUsername(), user.getPassword());

        if (authenticatedUser != null) {
            session.setAttribute("user", authenticatedUser);
            System.out.println("Session ID: " + session.getId()); // 打印 Session ID
            System.out.println("User in Session: " + authenticatedUser); // 打印用户信息
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Login failed");
        }
    }


    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUser/{id}")
    @ResponseBody
    public List<User> getUser(@PathVariable Integer id) {
        log.info("id:{}", id);
        List<User> user = userService.getUser(id);
        return user;
    }
    /**
     * 获取所有部门信息
     * @return
     */
    @GetMapping("/getAllDepartment")
    @ResponseBody
    public List<Department> getAllDepartment(){
        List<Department> allDepartment = userService.getAllDepartment();
        return allDepartment;
    }

    /**
     * 获取所有医生信息
     * @return
     */
    @GetMapping("/getAllDoctor")
    @ResponseBody
    public List<Doctor> getAllDoctor(){
        List<Doctor> allDoctor = userService.getAllDoctor();
        return allDoctor;
    }

    /**
     * 根据部门id获取医生信息
     * @param departmentId
     * @return
     */
    @GetMapping("/getDoctorsByDepartmentId/{departmentId}")
    @ResponseBody
    public List<Doctor> getDoctorsByDepartmentId(@PathVariable Long departmentId) {
        return userService.getDoctorsByDepartmentId(departmentId);
    }

    /**
     * 显示医生信息页面
     * @return
     */
    @GetMapping("/doctors")
    public String showDoctors() {
        return "doctor";  // 返回 doctor.html 模板
    }
    /**
     * 根据医生id获取排班信息
     * @param doctorId
     * @return
     */
    @GetMapping("/getScheduleByDoctorId/{doctorId}")
    @ResponseBody
    public List<Schedule> getScheduleByDoctorId(@PathVariable Long doctorId){
        return userService.getScheduleByDoctorId(doctorId);
    }
    /**
     * 显示排班信息页面
     * @return
     */
    @GetMapping("/schedule")
    public String showSchedulePage() {
        return "schedule";
    }
    /**
     * 添加预约
     * @param schedule
     * @param session
     * @return
     */
    @PostMapping("/addAppointment")
    public ResponseEntity<?> addAppointment(@RequestBody Schedule schedule,
            HttpSession session) {
        log.info("schedule:{}", schedule);
        if (schedule.getAvailableNumber() > 0){
            userService.addAppointment(schedule,session);
            userService.updateAvailableNumber(schedule);
        }

        return ResponseEntity.ok().body(Collections.singletonMap("success", true));
    }

    /**
     * 更新预约剩余人数
     * @param schedule
     */
    @PutMapping("/updateAvailableNumber")
    public void updateAvailableNumber(@RequestBody Schedule schedule){
        userService.updateAvailableNumber(schedule);
    }


    /**
     * 添加排队预约
     * @param appointment
     * @param schedule
     */
    @PostMapping("/addWaitingAppointment")
    public void addWaitingAppointment(@RequestBody Appointment appointment,Schedule schedule) {
        Waiting waiting = userService.addWaitingAppointment(appointment,schedule);
    }


    /**
     * 显示排队预约页面
     * @return
     */
    @GetMapping("/appointment")
    public String showappointmentPage() {
        return "appointment";
    }


    /**
     * 获取排队预约信息
     * @return
     */
    @GetMapping("/getAppointmentDetails")
    @ResponseBody
    public List<AppointmentInfo> getAppointmentDetails(HttpSession session){
        return userService.getAppointmentDetails(session);
    }

    /**
     * 显示排队预约页面
     * @return
     */
    @GetMapping("/waiting")
    public String showWaitingPage() {
        return "waiting";
    }

    /**
     * 获取排队
     * @return
     */
    @PostMapping("/getAppointmentWaiting")
    @ResponseBody
    public ResponseEntity<?> getAppointmentWaiting(@RequestBody AppointmentInfo appointmentInfo){
        List<WaitingDetail> waitingList = userService.getAppointmentWaiting(appointmentInfo);
        return ResponseEntity.ok()
                .header("X-Department-Name", appointmentInfo.getDepartmentName())
                .body(waitingList);
    }
}
